package com.ourway.dtos;

import cn.hutool.core.annotation.Alias;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 *
 * @author liuweiheng
 * @since 2021/12/12
 */
@Data
@Accessors(chain = true)
@Builder
public class FeiShuDeptUpdateDTO implements Serializable {

    @JsonProperty("name")
    @Alias("name")
    private String name;

    @JsonProperty("parent_department_id")
    @Alias("parent_department_id")
    private String parentDepartmentId;

    public void setNoneUpdatePropertyNull(FeiShuDeptUpdateDTO old) {
        Class<FeiShuDeptUpdateDTO> clazz = FeiShuDeptUpdateDTO.class;
        List<Field> allFields = new ArrayList<>(100);
        allFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
        allFields.forEach(field -> {

            // 设置字段可访问， 否则无法访问private修饰的变量值
            field.setAccessible(true);
            try {
                // 获取字段名称
                String fieldName = field.getName();

                // 获取指定对象的当前字段的值
                String oldValue = String.valueOf(field.get(old));
                String newValue = String.valueOf(field.get(this));
                if (oldValue.equals(newValue)) {
                    field.set(this, null);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
    }

}
